﻿#ifndef LIBSSHUTILS_H
#define LIBSSHUTILS_H

#include <QObject>
#include "libssh/libssh.h"
#include "libssh/callbacks.h"
#include "libsshParams.h"
#include <QSocketNotifier>
#include <QTcpSocket>
#include <QThread>
#include <QTimer>
#include <libssh/sftp.h>
#include <QString>

class libsshUtils:public QThread
{
    Q_OBJECT
    enum {
        DISCONNECTED,
        CONNECTED,
    };
public:
    libsshUtils(QObject *parent=nullptr,libsshParams *params=nullptr);
    ~libsshUtils();
    bool b_keepAlive=true;
    libsshParams *sshParams=nullptr;
protected:
    void run() override;
private:
    bool initFlag=false;
    int connectState=DISCONNECTED;
    QByteArray m_receiveData;
    
    //定时重连服务器
    QTimer* m_reconnectTimer=nullptr;
    //重连计时
    int m_refreshTime = 3000;
    ssh_session session=nullptr;
    ssh_channel channel=nullptr;
    QSocketNotifier *read_notifier=nullptr;
    QByteArray sendDataByte;
    QList<libsshSftpFileInfo> listInfo;
    void connectSSh();
    
    int authenticate_kbdint(ssh_session session, const char *password);
    

    //sftp
    
    char buffer[8192];
    sftp_session sftp=NULL;
    sftp_attributes file=NULL;
    QString localSavePath="";
    QString currentPath="";

    QString getPermission(uint32_t permissions);
    QString getSize(quint64 size);
    QString getType(uint32_t permissions);
    QStringList downloadListPath;
    QStringList uploadListPath;
    int row=0,col=0;//备份当前显示行列大小，防止每次更新太多次
public slots:
    void select_loop();
    /**
      * @brief initializeFunction:创建套接字、计时器、功能槽函数
      */
     void initializeFunction();
     /**
      * @brief getConnectStatus：获得连接状态
      * @return
      */
     int getConnectStatus();
     /**
      * @brief reConnectTcpServer：重连服务器
      * @return
      */
     bool reConnectTcpServer();
     /**
      * @brief sendData：发送有数据给服务器
      * @param data
      */
     void sendData(QByteArray data);
     void disconnectSSH();
#if 0
     int upload(QString path);
     int downLoadFile(QStringList path);//下载文件
     int upLoadFile(QStringList path);//上传文件
     int download(QString path);
    void sftp_connect();

    void sftp_list_dir_slots(QString path);//显示当前目录下的内容

    void startDownLoadFile(QStringList path,QString localSavePath);

    void startUpLoadFile(QStringList path);

    void sftpGetCurrentPathSlot();

    void sftpRenameFileSlots(QString oldPath,QString newPath);//重命名文件

    void sftpMkdirDirSlots(QString newFileDirPath,quint32 permission,bool openDir);//新建目录

    void sftpRemoveSlots(QString path);//删除目录或文件

    void sftpNewFileSlots(QString newFileName,quint32 permission);//新建文件
#endif
    void sshConnectStateSlots(bool state);

    void sshResizeWindowSlots(int height,int witdh);
signals:
    void sshConnectedSignal();
    
    void receiveData(QByteArray data);

    void sessionStateSignal(bool state);

    void sftpStateSignal(bool state);

    void errorMsgSignal(QString sftpError);

    void sftpErrorMsgSignal(QString sftpError);

    void sftpDirChangedSignal( QList<libsshSftpFileInfo> fileInfoList);//目录内内容信号

    void sftpSetCurrentPathSignal(QString path);//发送当前用户所在目录

    void sftpSetTrackCurrentPathSignal(QString path);//发送当前跟随用户所在目录

    void sftpDownLoadSizeSignal(QString path,quint64 totalsize,quint64 currentSize);//文件下载进度大小

    void sftpDownLoadFinished(QString path);//文件下载完成

    void sftpUpLoadSizeSignal(QString path,quint64 totalsize,quint64 currentSize);//文件上传进度大小

};

#endif // libsshUtils_H
